<template>
  <transition name="video-fade">
    <div class="video-dialog" v-if="showDialog" @click.stop>
      <span class="close" @click.stop="hide">
        <a-icon type="close-circle"></a-icon>
      </span>
      <div class="video-content" @click.stop>video</div>
    </div>
  </transition>
</template>
<script>
import DPlayer from 'dplayer'

export default {
  name: 'videoDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    videoData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data () {
    return {
      site: 'http://video.fanchao.site/',
      dp: null
    }
  },
  watch: {
    showDialog (val) {
      if (val) {
        this.initVideo(this.videoData)
      } else {
        this.dp.pause()
      }
    }
  },
  mounted () {
  },
  methods: {
    initVideo (item) {
      this.$nextTick(() => {
        this.dp = new DPlayer({
          container: document.getElementsByClassName('video-content')[0],
          screenshot: true,
          autoplay:	true,
          loop: false,
          video: {
            url: `${this.site}${item.videoKey}`,
            pic: `${this.site}${item.coverKey}`,
            thumbnails: `${this.site}${item.coverKey}`
          }
        })
        this.dp.play()
      })
    },
    hide () {
      this.$emit('closeDialog')
    }
  }
}
</script>

<style lang="scss" scoped>
  .video-dialog{
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 2001;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: rgba(0, 0, 0, 0.3);
    &.video-fade-enter-active{
      animation: video-fadein 0.3s;
      .video-content{
        animation: video-zoom .3s
      }
    }
    &.video-fade-leave-active{
      animation: video-fadein 0.3s reverse;
      .video-content{
        animation: video-zoom .3s reverse;
      }
    }
    .close{
      display: inline-block;
      width: 100px;
      height: 100px;
      background: #fff;
      border-radius: 50px;
      font-size: 30px;
      cursor: pointer;
      position: fixed;
      right: 0px;
      top: 0px;
      display: flex;
      justify-content: center;
      align-items: center;
      transform: translate(50%, -50%);
      i{
        transform: translate(-70%, 70%);
      }
    }
    .video-content{
      width: 850px;
      height: 500px;
      border-radius: 6px;
      background: #fff;
    }
  }

  @keyframes video-fadein{
    0%{
      opacity: 0
    }
    100%{
      opacity: 1
    }
  }

  @keyframes video-zoom{
    0%{
      transform: scale(0)
    }
    50%{
      transform: scale(1.1)
    }
    100%{
      transform: scale(1)
    }
  }
</style>
